function [ ] = detectForegroundCMV( path, filename, frames, initframes, shadow )

    K = 3.5;
    lernrate = 0.01;

    %figure('name', 'Ausgabe');

    imgname_last = sprintf('%s%s_%04d.jpeg',path,filename,0);
    img_last = imread(imgname_last);

    img_sum = zeros(size(img_last));
    
    %% calc Median
    for idx=0:initframes-1

        imgname_last = sprintf('%s%s_%04d.jpeg',path,filename,idx);
        img_last = imread(imgname_last);

        img_sum = img_sum + double(img_last);
    end

    img_median = img_sum/(initframes);

    if shadow==1
        img_median_rgb = img_median(:,:,1) + img_median(:,:,2) + img_median(:,:,3);

        img_median_nrgb = img_median;
        img_median_nrgb(:,:,1) = img_median_nrgb(:,:,1)./img_median_rgb;
        img_median_nrgb(:,:,2) = img_median_nrgb(:,:,2)./img_median_rgb;
        img_median_nrgb(:,:,3) = img_median_nrgb(:,:,3)./img_median_rgb;
    
    end 
    
    imgname_last = sprintf('%s%s_%04d.jpeg',path,filename,0);
    img_last = imread(imgname_last);
    img_sum = zeros(size(img_last));

    %% calc Varianz
    for idx=0:initframes-1 

       imgname_last = sprintf('%s%s_%04d.jpeg',path,filename,idx);
       img_last = imread(imgname_last);

       img_sum = img_sum + (double(img_last)-img_median).^2;
    end

    img_var = sqrt(img_sum/(initframes))*K;

    %% calc Vorder/Hintergrundsegmentierung
    for idx=initframes:initframes+frames-1

        imgname_current = sprintf('%s%s_%04d.jpeg',path,filename,idx);
        img_current = double(imread(imgname_current));

        if shadow==1
            img_current_rgb = img_current(:,:,1) + img_current(:,:,2) + img_current(:,:,3);

            img_current_nrgb = img_current;
            img_current_nrgb(:,:,1) = img_current_nrgb(:,:,1)./img_current_rgb;
            img_current_nrgb(:,:,2) = img_current_nrgb(:,:,2)./img_current_rgb;
            img_current_nrgb(:,:,3) = img_current_nrgb(:,:,3)./img_current_rgb;

            img_current_nrgb_temp = abs(img_current_nrgb - img_median_nrgb);

            img_current_nrgb_shadow = (and(and(and(img_current_nrgb_temp(:,:,1)<0.1,img_current_nrgb_temp(:,:,2)<0.1),img_current_nrgb_temp(:,:,3)<0.1),img_current_rgb<img_median_rgb*0.7));
        end
        
        diff = abs(double(img_current) - img_median);
        %imshow(uint8(diff));
        diffLogic = diff > img_var;
        diff_result = (or(or(diffLogic(:,:,1),diffLogic(:,:,2)),diffLogic(:,:,3)));
        
        se = strel('disk',3);
        diff_result = medfilt2(diff_result,[5 5]);
        diff_result = imopen(diff_result,se);

           if shadow==1
               diff_result = diff_result - im2bw(img_current_nrgb_shadow);
           end
        imwrite(diff_bw, sprintf('%sSeg_%s_%04d.png',path,filename,idx), 'BitDepth',1);

       % imshow(diff_result);
       % pause(0.01);
        
        lernrate_vordergrund = 0.001;
        lernrate_hintergrund = 0.01;
        
        img_var(:,:,1) = (1-lernrate_hintergrund)*img_var(:,:,1).*not(diff_result) + lernrate_hintergrund*abs(double(img_current(:,:,1))-img_median(:,:,1)).*not(diff_result) + (1-lernrate_vordergrund)*img_var(:,:,1).*(diff_result) + lernrate_vordergrund*abs(double(img_current(:,:,1))-img_median(:,:,1)).*(diff_result);
        img_var(:,:,2) = (1-lernrate_hintergrund)*img_var(:,:,2).*not(diff_result) + lernrate_hintergrund*abs(double(img_current(:,:,2))-img_median(:,:,2)).*not(diff_result) + (1-lernrate_vordergrund)*img_var(:,:,2).*(diff_result) + lernrate_vordergrund*abs(double(img_current(:,:,2))-img_median(:,:,2)).*(diff_result);
        img_var(:,:,3) = (1-lernrate_hintergrund)*img_var(:,:,3).*not(diff_result) + lernrate_hintergrund*abs(double(img_current(:,:,3))-img_median(:,:,3)).*not(diff_result) + (1-lernrate_vordergrund)*img_var(:,:,3).*(diff_result) + lernrate_vordergrund*abs(double(img_current(:,:,3))-img_median(:,:,3)).*(diff_result);
        
        img_median(:,:,1) = (1-lernrate_hintergrund)*img_median(:,:,1).*not(diff_result) + lernrate_hintergrund*double(img_current(:,:,1)).*not(diff_result) + (1-lernrate_vordergrund)*img_median(:,:,1).*(diff_result) + lernrate_vordergrund*double(img_current(:,:,1)).*(diff_result);
        img_median(:,:,2) = (1-lernrate_hintergrund)*img_median(:,:,2).*not(diff_result) + lernrate_hintergrund*double(img_current(:,:,2)).*not(diff_result) + (1-lernrate_vordergrund)*img_median(:,:,2).*(diff_result) + lernrate_vordergrund*double(img_current(:,:,2)).*(diff_result);
        img_median(:,:,3) = (1-lernrate_hintergrund)*img_median(:,:,3).*not(diff_result) + lernrate_hintergrund*double(img_current(:,:,3)).*not(diff_result) + (1-lernrate_vordergrund)*img_median(:,:,3).*(diff_result) + lernrate_vordergrund*double(img_current(:,:,3)).*(diff_result);
        
        imshow(img_median);
        
        if shadow==1
            img_median_nrgb = (1-lernrate)*img_median_nrgb + lernrate*img_current_nrgb;
            img_median_rgb = (1-lernrate)*img_median_rgb + lernrate*img_current_rgb;
        end

    end

end

